package com.hoshiibuy.purchase.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.purchase.entity.Podetail;
import com.hoshiibuy.purchase.entity.param.PodetailQueryParam;
import com.hoshiibuy.purchase.entity.param.PodetailUpdateParam;
import com.hoshiibuy.purchase.entity.param.PodetailAddParam;
import com.hoshiibuy.purchase.service.PodetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-15
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/podetail")
@Api(value = "采购", tags = {"采购单详情管理"})
public class PodetailController {
    @Autowired
    private PodetailService podetailService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询
    */
    @ApiOperation(value = "分页查询")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid PodetailQueryParam param) {
        try {
            log.info("分页查询");
            QueryWrapper<Podetail> queryWrapper = new QueryWrapper<Podetail>();
            queryWrapper.eq(param.getPoid() != null, "poid", param.getPoid());
            queryWrapper.eq(param.getOrderDetailid() != null, "order_detailid", param.getOrderDetailid());
            queryWrapper.eq(StringUtils.isNotBlank(param.getSpuName()), "spu_name", param.getSpuName());
            queryWrapper.eq(StringUtils.isNotBlank(param.getSkuName()), "sku_name", param.getSkuName());
            queryWrapper.eq(StringUtils.isNotBlank(param.getSpuNo()), "spu_no", param.getSpuNo());
            queryWrapper.eq(param.getSpuId() != null, "spu_id", param.getSpuId());
            queryWrapper.eq(StringUtils.isNotBlank(param.getSkuNo()), "sku_no", param.getSkuNo());
            queryWrapper.eq(param.getSkuId() != null, "sku_id", param.getSkuId());
            queryWrapper.eq(param.getRate2() != null, "rate2", param.getRate2());
            queryWrapper.eq(param.getRateamount() != null, "rateamount", param.getRateamount());
            queryWrapper.eq(param.getAmount() != null, "amount", param.getAmount());
            queryWrapper.eq(StringUtils.isNotBlank(param.getUnit()), "unit", param.getUnit());
            queryWrapper.eq(StringUtils.isNotBlank(param.getSku()), "sku", param.getSku());
            queryWrapper.eq(param.getUnitprice() != null, "unitprice", param.getUnitprice());
            queryWrapper.eq(param.getQuantity() != null, "quantity", param.getQuantity());
            queryWrapper.eq(param.getStatus() != null, "status", param.getStatus());
            queryWrapper.eq(param.getShippongcost() != null, "shippongcost", param.getShippongcost());
            queryWrapper.eq(param.getOthercost() != null, "othercost", param.getOthercost());
            queryWrapper.eq(param.getRate() != null, "rate", param.getRate());
            queryWrapper.eq(param.getRatefee() != null, "ratefee", param.getRatefee());
            queryWrapper.eq(param.getTotalamount() != null, "totalamount", param.getTotalamount());
            IPage<Podetail> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(Podetail.class));
            return Response.data(podetailService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询异常", e);
            return Response.failed("分页查询异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(podetailService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public Response save(@RequestBody @Valid PodetailAddParam param) {
        try {
            Podetail data = BeanUtils.covertType(param, Podetail.class);
            return Response.data(podetailService.save(data));
        } catch (ServiceException e) {
            log.error("新增异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public Response update(@RequestBody @Valid PodetailUpdateParam param) {
        try {
            Podetail data = BeanUtils.covertType(param, Podetail.class);
            return Response.data(podetailService.updateById(data));
        }catch (ServiceException e) {
            log.error("修改异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改异常", e);
            return Response.failed();
        }
    }

    /**
     * @param id
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 删除
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        try {
            Podetail data=podetailService.getById(id);
            if(data==null){
                log.error("删除异常，无法找到记录");
                return Response.failed("删除异常，无法找到记录");
            }
            podetailService.deleteById(data);
            return Response.success("删除成功");
        }catch (ServiceException e) {
            log.error("删除异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("删除异常", e);
            return Response.failed();
        }
    }

}
